<?php 
/**
 * 原产品模版sql字段类型功能实现.
 * */
include_once('public_function.php');
class PublicProducts extends PublicFunction{
	var $db, $web_fage;
	function __construct( $db, $web_fage ){
		$this->db = $db;
		$this->web_fage = $web_fage;
	}
	
	/**
	 * 最后一条产品评论信息
	 * */
	function productLastReviewsInfo( $get ){	
		
		
		$id = (int) $get['产品ID'];

		$sql = "SELECT review, username FROM product_reviews_detail 
				WHERE pid = {$id} AND web_type = '{$this->web_fage}' AND check_state = 1 LIMIT 1 ";
		$rows = $this->db->getOneRs($sql);
		return $rows;
	}
	
	/**
	* 产品老价格以及新价格（包括是否按年收费）
	* @param   array      		 $get       字段数组
	* @return  string
	*/
	 function productPriceOldAndCurr($get) {

		
		$id = (int) $get['产品ID'];
		
		$arr = PublicProducts::productPrice($id);//得到价格信息
		
		$old = empty($arr['old']) ? '' : $arr['old'].$arr['year'];
		$curr = $arr['curr_int'].'.'.$arr['curr_dec'].$arr['year'];
		
		return array('old' => $old, 'curr' => $curr );	
	}
	
	/**
	* 产品绑定信息
	* @param   array      		 $get       字段数组
	* @return  string
	*/
	function bindingRelatedProductInfo($get) {

		
		$id = (int) $get['产品ID'];
		
		$row = $this->productInfo($id);
		$p_cbsid  = $row['product_cbsid'];

		$product_bind_data = json_decode(file_get_contents('http://cbs.wondershare.cn/interface.php?m=recommend'));
		$p_bind_cbsid = '';//定义产品绑定的cbsid
		if (empty($product_bind_data->$p_cbsid)) {
			return '';
		}
		$data_bind =  $product_bind_data->$p_cbsid;
		
		if (!empty($data_bind)) {
			foreach ($data_bind  AS $keys=>$values) {
				
				$binds = explode('_',$keys);
				$p_bind_cbsid .= empty($p_bind_cbsid ) ? $binds[0]: ','.$binds[0];
			}
		}
		
		
		
		$sql = "SELECT won_p.id, p.product_intro, p.product_id, p.product_name, p.product_box_middle, p.product_type
				FROM product p LEFT JOIN won_product won_p ON won_p.won_product_id = p.product_id
				WHERE FIND_IN_SET(p.product_cbsid,'{$p_bind_cbsid}') AND won_p.won_product_delete = 0 AND won_p.web_type = '{$this->web_fage}'
				ORDER BY FIND_IN_SET(p.product_cbsid,'{$p_bind_cbsid}') ";
		$result = $this->db->query($sql);
		
		unset($binds, $data_bind, $product_bind_data, $row);
		
		return $result;
	}
	
	
	/**
	 * Icon图标大地址
	 * */
	function iconBigImage( $get ){	
		
		$id =   (int) $get['产品ID'];
		$sql = "SELECT p.product_icon_max FROM won_product won_p
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE won_p.id = {$id} AND won_p.won_product_delete = 0";
		$product_icon_max = $this->db->getOne($sql);
		return $product_icon_max ? $product_icon_max : '';
	}
	
	/**
	* 根据won_product表中的id取得product表中产品信息，供子类调用
	*/
	protected function productInfo( $id ){
		$sql = "SELECT p.* FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id WHERE id = '{$id}' AND won_p.won_product_delete = 0 ";
		$row = $this->db->getOneRs( $sql );
		return $row;
	}
	
	/**
	 * 加系统产品名称
	 * */
	function productNameHadOs( $get ){
		$id = $get['产品ID'];
		$name = trim($get['产品名称']);

		$row = $this->productInfo( $id );
		$os = $row['product_os'];

		if( $os == 'Win' ){
			if( !strpos( $name, 'for Windows') ) $name .= ' for Windows';
		}
		return $name;
	}
	
	
	/**
	* 根据产品id取产品所属品牌
	*/
	function productBrand( $get ){
		
		$id = $get['产品ID'];
		$row = $this->productInfo( $id );
		return isset($row['product_brand']) ? $row['product_brand'] : '';
	}

	/**
	* 产品核心功能
	*/
	function productCore( $get ){	
		
		$id = $get['产品ID'];
		$web_type = $get['web_type'];
		
		$sql = "SELECT p.product_key_feature FROM won_product won_p
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE id = '{$id}' AND won_p.won_product_delete = 0 ";
		$d = $this->db->getOne($sql);
		return $d ? $d : '';
	}


	/**
	 * 系统支持短
	 * */
	function sysSupportShot( $get ){	
		
		$id = $get['产品ID'];
		$sql = "SELECT p.product_support_intro FROM won_product won_p
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE won_p.id = {$id} AND won_p.won_product_delete = 0";
		$d = $this->db->getOne($sql);
		return $d ? $d : '';
	}
	
	/**
	 * 系统支持长
	 * */
	function sysSupportLong( $get ){	
		
		$id = $get['产品ID'];
		$sql = "SELECT p.product_support_content FROM won_product won_p
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE won_p.id = {$id} AND won_p.won_product_delete = 0";
		$c = $this->db->getOne($sql);
		return $c ? $c : '';
	}

	/**
	* 根据产品id取产品标语
	*/
	function productWord( $get ){
		$id = $get['产品ID'];	
		$row = $this->productInfo( $id );
		$product_ad = $row['product_ad'];		
		return $product_ad ? $product_ad : '';
	}

	/**
	* 根据产品id获取产品对比表
	* @return  string
	*/
	function compareTable( $get )
	{	
		$id = $get['产品ID'];
		$web_type = $get['web_type'];
		
		$sql = "SELECT p.product_contrast_table FROM won_product won_p
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE id = '{$id}'";
		$table = $this->db->getOne($sql);
		return $table ? $table : '';
	}
	
	/**
	 * 取得guide内容
	 * */
	function getGuide( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT p.product_guide_content FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE id = '{$id}'";
		$guide = $this->db->getOne( $sql );
		return $guide ? $guide : '';
	}
	
	/**
	 * 取得Tech-Spec内容
	 * */
	function getTech( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT p.techspec_content_table FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id
				WHERE id = '{$id}'";
		$tech = $this->db->getOne( $sql );
		return $tech ? $tech : '';
	}
	
	/**
	 * 右侧视频弹出html，供子类调用
	 * */
	protected function rightVideoPopup( $id, $site = 'ws' ){
	
		$sql = "SELECT p.product_video_promo,p.video_width,p.video_higth,p.product_id FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id 
				WHERE id = '{$id}'";
		$row = $this->db->getOneRs($sql);
		
		if($row['product_video_promo'] == '') return '';

		$video_arr = explode('.',$row['product_video_promo']);
		$video_type = strtolower($video_arr[count($video_arr)-1]);

		$str = '';
		
		if( $site == 'ws' ){
			$swf_url = 'http://www.wondershare.com/pro/demo/FlvPlayerV2009.swf';
		}elseif( $site == 'iskysoft' ){
			$swf_url = 'http://www.iskysoft.com/images/screencast/player-new.swf';
		}elseif( $site == 'aimersfot' ){
			$swf_url = 'http://www.aimersoft.com/demo/flvplayer.swf';
		}
		
		
		if($video_type == 'swf'){
		    $video = '<embed width="'.max($row['video_width'],560).'" height="'.$row['video_higth'].'" flashvars="width='.max($row['video_width'],560).'&height='.$row['video_higth'].'&file='.$row['product_video_promo'].'&backcolor=0xff8c00&frontcolor=0xE2F0FE" allowfullscreen="true" 
		    quality="high" name="mediaplayer" id="mediaplayer" style="undefined" src="'.$row['product_video_promo'].'" type="application/x-shockwave-flash"></embed>';
		}else if($video_type == 'flv'){
		    $video = '<embed width="'.max($row['video_width'],560).'" height="'.$row['video_higth'].'" flashvars="width='.max($row['video_width'],560).'&height='.$row['video_higth'].'&file='.$row['product_video_promo'].'&backcolor=0xff8c00&frontcolor=0xE2F0FE&wmode=Opaque"
		    allowfullscreen="true" quality="high" name="mediaplayer" id="mediaplayer" style="undefined" src="'.$swf_url.'" type="application/x-shockwave-flash"></embed>';
		}else if($video_type == 'mov' || $video_type == 'm4v'){
			$video = '<embed src="'.$row['product_video_promo'].'" width="'.max($row['video_width'],550).'" height="'.$row['video_higth'].'"></embed>';
		}else if (substr_count($row['product_video_promo'],'http://www.youtube.com/') >0 ){
		     $url = str_replace('/embed/','/v/', $row['product_video_promo']);
		     $video = '<object width="640" height="390"><param name="movie" value="'.$url.'"></param>
		     <param name="allowFullScreen" value="true"></param>
		     <param name="allowscriptaccess" value="always"></param>
		     <embed src="'.$url.'" type="application/x-shockwave-flash" width="640" height="390" allowscriptaccess="always" allowfullscreen="true"></embed>
		     </object>';
		}
		
		$str .= '<script type="text/javascript"> 
		     $(function(){
			     $(".videoDemo").fancybox({
					 \'width\'				: 600,
					 \'height\'				: 480,
					 \'autoScale\'			: false,
					 \'transitionIn\'		: \'none\',
					 \'transitionOut\'		: \'none\'
				});
			});
			</script> ';
		
		$str .= '<div id="video-demo" style="display:none">'.$video.'</div>';
		
		return $str;
	}
	
	
	/**
	 * 产品价格
	 * */
	protected function productPrice( $id ) {
	
		$sql = "SELECT won_product_id FROM `won_product` WHERE id = '{$id}'";
		
		$product_id = $this->db->getOne( $sql );
		
		$sql = "SELECT count(*) AS total FROM `license` 
				WHERE `license_id` = 11 AND `website_display` = 1 AND `license_delete` = 0 AND `product_id` = '{$product_id}'";
		$total = $this->db->getOne($sql);
		
		$arr = array('curr_int'=>'','curr_dec'=>'','year'=>'','old'=>'','cart_url'=>'','swreg_url'=>'');
		if( $total ){			
			$sql = "SELECT `product_is_year_charge` FROM product WHERE `product_delete` = 0 AND `product_id` = '{$product_id}'";
			$product_year = $this->db->getOne($sql);

			$price_one = get_price_license( $product_id, 11);
			$product_price = explode('.', $price_one['curr']);
	
			
			$arr['curr_int'] = '$'.$product_price[0];//目前价格整数部分
			$arr['curr_dec'] = $product_price[1];//目前价格小数部分
			$arr['year'] = $product_year ? '/Year' : '';//是否一年的费用
			$arr['old']	= isset($price_one['unique']) ? '$'.$price_one['old'] : '';//促销前价格
			$arr['cart_url'] = $price_one['cart_url'];
		   	$arr['swreg_url'] = $price_one['swreg_url'];
		}
		return $arr;
	}

	//note 产品头部配图
	function topImage( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT top_image FROM won_product WHERE id = '{$id}' AND won_product_delete = 0";
		$url = $this->db->getOne( $sql );
		return $url ? $url : '';
	}
	
	//note 产品大盒子地址
	function productBigBox( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT p.`product_box_big` FROM `won_product` AS won
				LEFT JOIN `product` AS p ON won.won_product_id = p.product_id
				WHERE won.id = '{$id}' AND p.`product_delete` = 0";
		$url = $this->db->getOne( $sql );
		return $url ? $url : '';
	}

	//note 产品中盒子地址
	function productMidBox( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT p.`product_box_middle` FROM `won_product` AS won
				LEFT JOIN `product` AS p ON won.won_product_id = p.product_id
				WHERE won.id = '{$id}' AND p.`product_delete` = 0";
		$url = $this->db->getOne( $sql );
		return $url ? $url : '';
	}
	
	//note 产品小盒子地址
	function productSmallBox( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT p.`product_box_min` FROM `won_product` AS won
				LEFT JOIN `product` AS p ON won.won_product_id = p.product_id
				WHERE won.id = '{$id}' AND p.`product_delete` = 0";
		$url = $this->db->getOne( $sql );
		return $url ? $url : '';
	}
	
	//note 产品下载地址
	function productDownLoadUrl( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT won_product_id FROM `won_product` WHERE id = '{$id}'";
		$product_id = $this->db->getOne( $sql );
		
		$version = get_sale_version( $product_id );
		$url = $version['download_url'];
		return $url;
	}
	
	//note 产品大小（单位为M）
	function proudctSize( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT won_product_id FROM `won_product` WHERE id = '{$id}'";
		$product_id = $this->db->getOne( $sql );
			
		$version = get_sale_version( $product_id );
		$size = $version['product_size'];
		return $size;
	}
	
	
	//note 产品版本
	function productVersion( $get ){
		$id = $get['产品ID'];
		$sql = "SELECT won_product_id FROM `won_product` WHERE id = '{$id}'";
		$product_id = $this->db->getOne( $sql );
			
		$version = get_sale_version( $product_id );
		$version = $version['version_name'];
		return $version;
		
	}
	

	/**
	* 返回(产品评论模板)页面title生成
	* @param   array      		 $get       字段数组
	* @return  string
	*/
	function titleCreate($get) {
		$str = '';
		$id = $get['产品ID'];
		$sql = "SELECT p.product_name,p.product_variant 
				FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id 
				WHERE won_p.id = '{$id}'";
		$p = $this->db->getOneRs( $sql );
		
		$pname2 = $pname = $p['product_name'];
		$pv = $p['product_variant'];
		
		if ( trim(strtolower(substr( $pname, 0, 11 ))) == 'wondershare' ) {
			$pname2 = trim(substr( $pname, 11 ));
		}
		if ( trim(strtolower(substr( $pname, 0, 11 ))) == 'anypdftools' ) {
			$pname2 = trim(substr( $pname, 11 ));
		}
		if ( trim(strtolower(substr( $pname, 0, 13 ))) == 'applemacvideo' ) {
			$pname2 = trim(substr( $pname, 13 ));
		}
		if ( trim(strtolower(substr( $pname, 0, 8 ))) == 'iskysoft' ) {
			$pname2 = trim(substr( $pname, 8 ));
		}
		if ( trim(strtolower(substr( $pname, 0, 9 ))) == 'aimersoft' ) {
			$pname2 = trim(substr( $pname, 9 ));
		}
		
		$n = $pv ? $pv : $pname2;
		$str = $pname.' Review -- Best '.$n.' Review';
		return $str;	
	}

	/**
	* 返回(产品评论模板)页面描述生成
	* @param   array      		 $get       字段数组
	* @return  string
	*/
	function descriptionCreate($get) {		
		$str = '';
		$id = $get['产品ID'];
		
		$row = $this->productInfo( $id );
		
		$sql = "SELECT username, review FROM product_reviews_detail WHERE pid = '{$id}' ORDER BY did DESC LIMIT 1";
		$data = $this->db->getOneRs( $sql );
		
		if ( !empty( $data) ){
			$str = $row['product_name']." review by {$data['username']} :". (strlen($data['review']) > 70 ? substr( $data['review'], 0, 70 ).'...' : $data['review']);
		}else{
			$str = 'Share your experience with '.$row['product_name'];
		}		
		return $str;
	}
	
	//note 评论信息
	function reviews( $id, $url_reviews ) {
		
		$sql = "SELECT star_1,star_2,star_3,star_4,star_5,(star_1+star_2+star_3+star_4+star_5) AS all_num 
				FROM product_reviews 
				WHERE pid = {$id}";
		$s = $this->db->getOneRs($sql);
		
		$star_avg = 0;
		if( $s['all_num'] ) {
			$star_avg = ($s['star_1'] + 2*$s['star_2'] + 3*$s['star_3'] + 4*$s['star_4'] + 5*$s['star_5']) / $s['all_num'];
		}

		$str = '<div class="ratingArea" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
				<span class="review"><a href="'.$url_reviews.'">
				<meta itemprop="worstrating" content="1" /><meta itemprop="ratingValue" content="'.$star_avg.'" /><meta itemprop="bestRating" content="5" />Avaliações dos usuários<span class="small">(<span itemprop="ratingCount">'.
				($s['all_num']?$s['all_num']:0).'</span>)</span></a></span>
				<div class="rating">
				<ul id="star">
				<li><a href="javascript:;">1</a></li>
				<li><a href="javascript:;">2</a></li>
				<li><a href="javascript:;">3</a></li>
				<li><a href="javascript:;">4</a></li>
				<li><a href="javascript:;">5</a></li>
				</ul>
				
				</div>
				<div class="distr">
				<a href="javascript:void(0)" class="showBtn"></a>
				<div class="distrArea" style="display:none;">
				<span class="distrAreaTitle">Ratings Distrubution</span>
				<ul class="vote-box-list clearfix" id="appVoteBox">   
				<li class="vl-item" id="voteItem0" >
				<div class="vote-item-wrap"> <span class="xx">5 Star</span>
				<div class="litem"><em class="vright"></em><span></span><em class="vright"></em></div>
				<div class="data"></div>
				</div>
				</li>
				
				<li class="vl-item" id="voteItem1" >
				<div class="vote-item-wrap"> <span class="xx">4 Star</span>
				<div class="litem"><em class="vright"></em><span></span><em class="vright"></em></div>
				<div class="data"></div>
				</div>
				</li>
				
				<li class="vl-item" id="voteItem2" >
				<div class="vote-item-wrap"> <span class="xx">3 Star</span>
				<div class="litem"><em class="vright"></em><span></span><em class="vright"></em></div>
				<div class="data"></div>
				</div>
				</li>
				
				<li class="vl-item" id="voteItem3">
				<div class="vote-item-wrap"> <span class="xx">2 Star</span>
				<div class="litem"><em class="vright"></em><span></span><em class="vright"></em></div>
				<div class="data"></div>
				</div>
				</li>
				
				<li class="vl-item" id="voteItem4" >
				<div class="vote-item-wrap"> <span class="xx">1 Star</span>
				<div class="litem"><em class="vright"></em><span></span><em class="vright"></em></div>
				<div class="data"></div>
				</div>
				</li>
				</ul>
				</div>
				<a href="javascript:void(0)" class="hideBtn" style="display:none;"></a>
				</div>
				</div>';
		
		$str .= '<script type="text/javascript">
				var vote_data=[];
				vote_data["item_1"]='.(empty($s['star_5']) ? 0 : $s['star_5']).';
				vote_data["item_2"]='.(empty($s['star_4']) ? 0 : $s['star_4']).';
				vote_data["item_3"]='.(empty($s['star_3']) ? 0 : $s['star_3']).';
				vote_data["item_4"]='.(empty($s['star_2']) ? 0 : $s['star_2']).';
				vote_data["item_5"]='.(empty($s['star_1']) ? 0 : $s['star_1']).';
				
				var star_avg = '.(empty($star_avg) ? 0 : $star_avg).';
				</script>';
		return $str;
	}
	
}
?>